﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectFitus.Models;

namespace ProjectFitus.Controllers
{
	[HandleError]
	public class SpecialistController : Controller
    {
		IDBFitusRepository m_repo = new DBFitusRepository();

		protected override void OnException(ExceptionContext fc)
		{
			base.OnException(fc);
			FitusLogger.Instance.LogException(fc.Exception);

		}

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(FormCollection formdata)
        {
            return View();
        }
        //
        // GET: /Specialist/Details/5

        public ActionResult Details(int? id)
        {
			return View();
        }

        //
        // GET: /Specialist/Create

        public ActionResult Create()
        {
            SpecialistCreationModel model = new SpecialistCreationModel();
            model.professionDropDownList = m_repo.GetAllProfessionsViaSelectListItems();
			model.professionDropDownList.Insert(0, new SelectListItem { Value = "0", Text = " -- Veldu hlutverk -- ", Selected = true });
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(FormCollection formData)
        {
            try
            {
                Request specRequest = new Request();

                System.Web.Security.MembershipUser CurrentUser = System.Web.Security.Membership.GetUser(User.Identity.Name);
				
				specRequest.requestedBy = m_repo.GetUserIdByAspNetUsrId((Guid)CurrentUser.ProviderUserKey);
                specRequest.requestDescription = formData["requestDescription"];
                specRequest.dateCreated = DateTime.Now;
                specRequest.statusId = 1;
                specRequest.requestType = 1;
				if (formData["professionId"] == "0") 
					RedirectToAction("Create"); //sendi sömu upplýsingar í formið aftur
                m_repo.CreateRequest(specRequest);
                Specialist speciali = new Specialist();
				speciali.professionId = int.Parse(formData["professionId"]);
				speciali.id = specRequest.requestedBy;
                speciali.specialty = formData["specialty"];
                speciali.isActive = false;
                

                speciali.dateCreated = DateTime.Now;
                m_repo.InsertNewSpecialist(speciali);
               

                //
                //
                //
                //
                // TODO: Add insert logic for specialist here
                var html = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView("temp"), ViewData, TempData, Response.Output), new ViewPage());
                return RedirectToAction("Index", "Home");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Specialist/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Specialist/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Specialist/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Specialist/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
